﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YanForum.Engine.Services
{
    public static class Post
    {
        static string connectionString = BaseService.ConnectionString;

        //by Tuan
        public static int CountAllPost(int? userId)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("CountAllPost", conn);

                userId = userId != null ? userId : -1;

                cmd.Parameters.AddWithValue("@userId", userId);
                cmd.CommandType = CommandType.StoredProcedure;
                var reader = cmd.ExecuteReader();
                int totalCount = 0;

                while (reader.Read())
                {
                    totalCount = (int)reader["TotalCount"];
                }
                reader.Close();

                return totalCount;
            }
        }

        //by Tuan
        public static YanForum.Engine.ViewModels.PostIndex SearchPost(YanForum.Engine.ViewModels.PostOtions options)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("GetSearchPost", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                //options.Search = !string.IsNullOrEmpty(options.Search) ? options.Search : "";
                cmd.Parameters.AddWithValue("@search", options.Search);
                cmd.Parameters.AddWithValue("@userId", options.UserId);
                cmd.Parameters.AddWithValue("@pageIndex", options.PageIndex);
                cmd.Parameters.AddWithValue("@pageSize", options.PageSize);

                var reader = cmd.ExecuteReader();

                var model = new YanForum.Engine.ViewModels.PostIndex();
                //model.TotalCount = CountAllPost(options.UserId);
                model.Options = options;
                model.ListPost = new List<ViewModels.PostItem>();

                reader.Read();
                int total = (int)reader["TotalCount"];
                model.TotalCount = total;

                // gets the second
                reader.NextResult();

                while (reader.Read())
                {
                    var item = new ViewModels.PostItem()
                    {
                        Id = int.Parse(reader["Id"].ToString()),
                        Title = reader["Title"].ToString(),
                        Content = reader["Content"].ToString(),
                        CreateUserId = int.Parse(reader["CreateUser"].ToString()),
                        CreateUserName = reader["CreateUserName"].ToString(),
                        ThumbFolder = reader["ThumbFolder"].ToString(),
                        ThumbName = reader["ThumbName"].ToString(),
                        DateUpdate = DateTime.Parse(reader["UpdateDate"].ToString())
                    };

                    model.ListPost.Add(item);
                }
                reader.Close();

                return model;
            }
        }

        //by Tuan
        public static YanForum.Engine.ViewModels.PostUpdate GetPostById(int Id)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("GetPostById", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@postId", Id);

                var reader = cmd.ExecuteReader();

                var model = new ViewModels.PostUpdate();

                while (reader.Read())
                {
                    model.Title = reader["Title"].ToString();
                    model.Content = reader["Content"].ToString();
                    model.ThumbFolder = reader["ThumbFolder"].ToString();
                    model.ThumbName = reader["ThumbName"].ToString();
                    model.ImagePath = reader["ThumbFolder"].ToString() + "/" + reader["ThumbName"].ToString();
                    model.Id = Id;
                }
                reader.Close();

                return model;
            }
        }

        //by Tuan
        public static YanForum.Engine.ViewModels.PostItem GetPostDetail(int Id)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("GetPostById", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@postId", Id);

                var reader = cmd.ExecuteReader();

                var model = new ViewModels.PostItem();

                while (reader.Read())
                {
                    model.Id = Id;
                    model.Title = reader["Title"].ToString();
                    model.Content = reader["Content"].ToString();
                    model.ThumbFolder = reader["ThumbFolder"].ToString();
                    model.ThumbName = reader["ThumbName"].ToString();
                    model.DateUpdate = DateTime.Parse(reader["UpdateDate"].ToString());
                    model.CreateUserId = int.Parse(reader["CreateUserId"].ToString());
                    model.CreateUserName = reader["UserName"].ToString();
                }
                reader.Close();

                return model;
            }
        }
        //by Tuan
        public static void UpdatePost(YanForum.Engine.ViewModels.PostUpdate model)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("UpdatePost", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", model.Id);
                cmd.Parameters.AddWithValue("@userId", model.UserId);
                cmd.Parameters.AddWithValue("@title", model.Title);
                cmd.Parameters.AddWithValue("@content", model.Content);
                cmd.Parameters.AddWithValue("@dateNow", DateTime.Now);
                cmd.Parameters.AddWithValue("@thumbFolder", model.ThumbFolder);
                cmd.Parameters.AddWithValue("@thumbName", model.ThumbName);
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }

        //by Tuan
        public static bool DeletePost(int postId)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("DeletePost", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@postId", postId);

                try
                {
                    cmd.ExecuteNonQuery();
                    conn.Close();

                    return true;
                }
                catch(Exception ex)
                {
                    return false;
                }
            }
        }
    }
}
